package com.lgl.mes.test;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;

public class PolygonDrawer {

    public static void main(String[] args) throws IOException {
        // 输入16个数字作为面积
        double[] areas = {10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40};

        // 计算总面积
        double totalArea = 0;
        for (double area : areas) {
            totalArea += area;
        }

        // 计算正方形的边长
        double sideLength = Math.sqrt(totalArea);

        // 创建一个BufferedImage对象
        BufferedImage image = new BufferedImage((int)sideLength, (int)sideLength, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();
        g2d.setBackground(Color.WHITE);
        g2d.clearRect(0, 0, (int)sideLength, (int)sideLength);

        // 定义颜色列表
        Color[] colors = new Color[] {
                Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW,
                Color.CYAN, Color.MAGENTA, Color.ORANGE, Color.PINK,
                Color.LIGHT_GRAY, Color.GRAY, Color.DARK_GRAY, Color.BLACK,
                Color.WHITE, Color.RED.darker(), Color.GREEN.darker(), Color.BLUE.darker()
        };

        // 生成16个多边形，每个多边形的面积与给定数组中的值一一对应
        List<Polygon> polygons = generatePolygons(sideLength, areas);

        for (int i = 0; i < 16; i++) {
            Polygon polygon = polygons.get(i);
            Color color = colors[i];

            // 绘制多边形
            g2d.setColor(color);
            g2d.fillPolygon(polygon.xpoints, polygon.ypoints, polygon.npoints);
            g2d.setColor(Color.BLACK);
            g2d.drawPolygon(polygon.xpoints, polygon.ypoints, polygon.npoints);

            // 添加面积文本
            //g2d.setColor(Color.BLACK);
            //g2d.drawString(String.format("%.1f", areas[i]), (int)(polygon.getBounds().getCenterX()), (int)(polygon.getBounds().getCenterY()));
        }

        // 保存图像到文件
        ImageIO.write(image, "png", new File("polygons_grid_java.png"));
        g2d.dispose();

        System.out.println("Image saved as polygons_grid_java.png");
    }

    // 生成16个多边形，每个多边形的面积与给定数组中的值一一对应
    private static List<Polygon> generatePolygons(double sideLength, double[] areas) {
        List<Polygon> polygons = new ArrayList<>();
        Random random = new Random();
        int numPolygons = areas.length;

        for (int i = 0; i < numPolygons; i++) {
            double area = areas[i];
            Polygon polygon = generatePolygonWithArea(sideLength, area);
            polygons.add(polygon);
        }

        return polygons;
    }

    // 生成具有指定面积的多边形
    private static Polygon generatePolygonWithArea(double sideLength, double targetArea) {
        Polygon polygon = new Polygon();
        Random random = new Random();

        while (true) {
            polygon.reset();
            int x = random.nextInt((int)sideLength);
            int y = random.nextInt((int)sideLength);

            // 生成四个随机顶点来构成多边形
            for (int i = 0; i < 4; i++) {
                int xOffset = random.nextInt(50) - 25;
                int yOffset = random.nextInt(50) - 25;
                polygon.addPoint(x + xOffset, y + yOffset);
            }

            // 计算多边形的面积
            double area = Math.abs(polygon.getBounds().getWidth() * polygon.getBounds().getHeight() / 2.0);

            // 判断生成的多边形是否符合要求
            if (Math.abs(area - targetArea) / targetArea < 0.1) {
                break;
            }
        }

        return polygon;
    }
}
